﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TP2
{
    public partial class AddBankForm : Form
    {
        BankClientForm Form;
        public AddBankForm(BankClientForm bankForm)
        {
            Form = bankForm;
            InitializeComponent();
        }

        private void AddBankForm_Load(object sender, EventArgs e)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            string client_type = "банк";
            selRate = dbWork.SelectFromCreditRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
            }
            else
            { dataGridView1.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selRate.title[j].ToString());
                dataGridView1[1, j].Value = selRate.procent[j].ToString();
                dataGridView1[2, j].Value = selRate.period[j].ToString();
                j++;
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            long Capital = Convert.ToInt64(textBoxCapital.Text);
            string Title = textBoxTitle.Text;
            string Inn = maskedTextBox1.Text;
            string Bik = maskedTextBoxBik.Text;
            string Type = textBoxType.Text;
            string Founder = textBoxFounder.Text;
            int credit = Convert.ToInt32(textBoxCredit.Text);
            DateTime date = dateTimePicker1.Value;
            int index=dataGridView1.CurrentRow.Index;
            Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
            int period = Convert.ToInt32(dataGridView1[2, index].Value);
            DBWorkClass dbWork = new DBWorkClass();
            dbWork.InsertFromBank(Title, Bik, Inn, Capital, Type, Founder, period, procent, credit, date);
            Form.RefresTable();
            this.Close();
        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
            LogicClass logicObj = new LogicClass();
            int index = dataGridView1.CurrentRow.Index;
            int summa = Convert.ToInt32(textBoxCredit.Text);                              
            Double procent = Convert.ToDouble(dataGridView1[1,index].Value);                    
            //labelInfo.Text = "сумма долга =";                    
            labelItog.Text = "сумма долга= "+logicObj.GetCreditSumm(summa, procent).ToString();
        }
    }
}
